package com.blobfish.fiction.common.pipeline;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.blobfish.fiction.book.BookChapterService;
import com.blobfish.fiction.book.service.impl.BookServiceImpl;
import com.blobfish.fiction.common.model.Book;
import com.blobfish.fiction.common.model.BookChapter;
import com.jfinal.aop.Aop;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.Date;

public class WujixiaoshuoPipeline implements Pipeline {
    static BookServiceImpl bookService = Aop.get(BookServiceImpl.class);
    static BookChapterService bookChapterService = Aop.get(BookChapterService.class);

    @Override
    public void process(ResultItems resultItems, Task task) {
        String url = resultItems.get("url");
        String title = resultItems.get("title");
        String value = resultItems.get("value");
        if (!StrKit.isBlank(url) && !StrKit.isBlank(title) && !StrKit.isBlank(value)) {
            // 替换value中不必要的文字
            String replaceRegSep = PropKit.use("a_little_config.txt").get("replaceRegSep");
            String replaceReg = PropKit.get("replaceReg");
            if (StrUtil.isNotBlank(replaceRegSep) && StrUtil.isNotBlank(replaceReg)) {
                String[] replaceRegs = replaceReg.split(replaceRegSep);
                for (String item : replaceRegs) {
                    value = StrUtil.replace(value, item, "");
                }
            }
            String[] urlSplits = url.split("/");
            String bookNo = urlSplits[urlSplits.length - 2];
            String author = "";
            String[] titleSplits = title.split(" ");
            String bookName = titleSplits[titleSplits.length - 1].split(" ")[2];
            String subName = titleSplits[titleSplits.length - 1].split(" ")[0];
            String chapterNum = urlSplits[urlSplits.length - 1];
            chapterNum = chapterNum.substring(0, chapterNum.indexOf("."));
            String chapterName;
            if (titleSplits.length == 1) {
                chapterName = titleSplits[0];
            } else {
                chapterName = titleSplits[titleSplits.length - 2];
            }
            if (!bookService.isExists(bookNo)) {
                Book book = new Book();
                book.setAddTime(new Date());
                book.setAuthor(author);
                book.setBookNo(bookNo);
                book.setName(bookName);
                bookService.addBook(book);
            }
            // 插入章节数据
            BookChapter bookChapter = new BookChapter();
            bookChapter.setBookNo(bookNo);
            bookChapter.setChapterNum(chapterNum);
            bookChapter.setContent(value);
            bookChapter.setName(subName);
            bookChapter.setChapterName(chapterName);
            String [] chapterNums = chapterNum.split("_");
            bookChapter.setChapterNumMain(Convert.toLong(chapterNums[0]));
            bookChapter.setChapterNumSub(Convert.toInt(chapterNums[1]));
            if (!bookChapterService.isExists(bookChapter.getChapterNum())) {
                bookChapterService.add(bookChapter);
            }
        }
    }
}
